package com.example.his.api.mis.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.mis.controller.form.appointment.*;
import com.example.his.api.mis.service.AppointmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController("MisAppointController")
@RequestMapping("/mis/appointment")
public class AppointmentController {
  
  private static final Logger log = LoggerFactory.getLogger(AppointmentController.class);
  @Resource
  private AppointmentService appointmentService;
  
  
  //  根据订单号查询预约信息
  @PostMapping("/searchByOrderId")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:SELECT" }, mode = SaMode.OR)
  public R searchByOrderId(@RequestBody @Valid SearchAppointmentByOrderIdForm form) {
    ArrayList <HashMap> list = appointmentService.searchByOrderId(form.getOrderId());
    return R.ok().put("result", list);
  }
  
  //   mis端查询客户预约数量
  @PostMapping("/searchByPage")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:SELECT" }, mode = SaMode.OR)
  public R searchByPage(@RequestBody @Valid SearchAppointmentByPageForm form) {
    int page = form.getPage();
    int length = form.getLength();
    int start = (page - 1) * length;
    Map param = BeanUtil.beanToMap(form);
    param.put("start", start);
    PageUtils pageUtils = appointmentService.searchByPage(param);
    return R.ok().put("result", pageUtils);
  }
  
  @PostMapping("/deteleByIds")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:DELETE" }, mode = SaMode.OR)
  public R deleteByIds(@RequestBody @Valid DeleteAppointmentByIdsForm form) {
    Integer[] ids = form.getIds();
    for(Integer id: ids) {
      if(id < 1)
        return R.error("id不能小于1");
    }
    int rows = appointmentService.deleteByIds(ids);
    return R.ok().put("result", rows);
  }
  
  //  查询身份证人是否今天有体检预约记录
  @PostMapping("/hasAppointInToday")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:UPDATE" }, mode = SaMode.OR)
  public R hasAppointInToday(@RequestBody @Valid HasAppointmentTodayForm form) {
    Map param = BeanUtil.beanToMap(form);
    int res = appointmentService.hasAppointInToday(param);
    
    return R.ok().put("result", res);
  }
  
  //  体检签到
  @PostMapping("/isCheckin")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:UPDATE" }, mode = SaMode.OR)
  public R isCheckin(@RequestBody @Valid CheckAppointmentForm form) {
    Map param = BeanUtil.beanToMap(form);
    
    boolean checkin = appointmentService.isCheckin(param);
    return R.ok().put("result", checkin);
  }
  
  
  //  体检项目
  @PostMapping("/searchGuidanceInfo")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:SELECT" }, mode = SaMode.OR)
  public R searchGuidance(@RequestBody @Valid SearchGuidanceInform form) {
    HashMap result = appointmentService.searchGuidanceInfo(form.getId());
    
    return R.ok().put("result", result);
  }
  
  @PostMapping("/updateStatusByUuid")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:UPDATE" }, mode = SaMode.OR)
  public R updateStatus(@RequestBody @Valid UpdateAppointmentStatusByUuidForm form) {
    Map <String, Object> param = BeanUtil.beanToMap(form);
    boolean bool = appointmentService.updateStatusByUuid(param);
    return R.ok().put("result", bool);
    
  }
  
  @PostMapping("/searchByUuid")
  @SaCheckPermission(value = { "ROOT", "APPOINTMENT:SELECT" }, mode = SaMode.OR)
  public R searchByUuid(@RequestBody @Valid SearchAppointmentByUuidForm form) {
    HashMap map = appointmentService.searchByUuid(form.getUuid());
    return R.ok().put("result", map);
  }
}
